$(function() {
	
	//初始化装载信件
	loadLetters(1);
	//加载各类记录数
	loadAllCounts();
	//加载标签组
	loadTags();
	//刷新数据
	refresh();
	
	//上一页
	$("#preview").bind("click",function() {
		var pageNum = parseInt($("#current").val());
		var sumPage = parseInt($("#sum").val());
		if (1 < pageNum && pageNum <= sumPage) {
			var flag = $("#flag").val();
			if ("all" == flag) {
				loadLetters(pageNum - 1);
			} else if ("undo" == flag) {
				loadUndoLetter(pageNum - 1);
			} else if ("level" == flag) {
				var level = $("#level").val();
				loadLevelLetters(pageNum - 1, level);
			} else if ("tag" == flag) {
				var tagName = $("#tagName").val();
				loadTagLetters(pageNum - 1, tagName);
			}
		}
	});
		
	//下一页
	$("#next").bind("click",function(){
		var pageNum = parseInt($("#current").val());
		var sumPage = parseInt($("#sum").val());
		if (1 <= pageNum && pageNum < sumPage) {
			var flag = $("#flag").val();
			if ("all" == flag) {
				loadLetters(pageNum + 1);
			} else if ("undo" == flag) {
				loadUndoLetter(pageNum + 1);
			} else if ("level" == flag) {
				var level = $("#level").val();
				loadLevelLetters(pageNum + 1, level);
			} else if ("tag" == flag) {
				var tagName = $("#tagName").val();
				loadTagLetters(pageNum + 1, tagName);
			}
		}
	});
	
	//加载未读信件
	$("#loadUndoLetter").bind("click",function(){
		loadUndoLetter(1);
	});
	
	//加载等级信件
	$("a[name='loadLevel']").each(function(){
		$(this).bind("click",function(){
			var level = $(this).attr("level");
			loadLevelLetters(1,level);
		});
	});

});

/**
 * 加载信件信息
 */
function loadLetters(pageNum){
	var url = "/JWLetter/letter/loadAllLetters";
	var data = {
		pageNum : pageNum
	};
	var successCallback = function(data){
		$("#flag").attr("value","all");
		loadDatas(data);
	};
	JWUtils.ajax(url, true, data, successCallback, null);
}

/**
 * 加载各类记录数
 */
function loadAllCounts(){
	var url = "/JWLetter/letter/loadLetterCounts";
	var successCallback = function(data){
		var letterCount = data.detail.letterCount;
		var undoLetterCount = data.detail.undoLetterCount;
		var levelOneLetterCount = data.detail.levelOneLetterCount;
		var levelTwoLetterCount = data.detail.levelTwoLetterCount;
		var levelThreeLetterCount = data.detail.levelThreeLetterCount;
		$("#letterCount").text(letterCount);
		$("#undoLetterCount").text(undoLetterCount);
		$("#levelOneLetterCount").text(levelOneLetterCount);
		$("#levelTwoLetterCount").text(levelTwoLetterCount);
		$("#levelThreeLetterCount").text(levelThreeLetterCount);
	};
	JWUtils.ajax(url, true, null, successCallback, null);
}

/**
 * 加载标签组
 */
function loadTags(){
	var url = "/JWLetter/letter/loadTags";
	var successCallback = function(data){
		var tags = data.detail.tags;
		var tagUl = $("#tags");
		tagUl.children().remove();
		if (tags != null && tags.length != 0) {
			for (var i = 0; i < tags.length; i++) {
				tagUl.append("<li><a href='javascript:;' tagName='"+tags[i].tagName+"' name='loadTagLetters'><i class='"+tags[i].tagClass+"'></i><font>"+tags[i].tagName+"</font></a></li>");
			}
		}
		$("a[name='loadTagLetters']").each(function(){
			$(this).bind("click",function(){
				var tagName = $(this).attr("tagName");
				loadTagLetters(1,tagName);
			});
		});
	};
	JWUtils.ajax(url, true, null, successCallback, null);
}

/**
 * 刷新数据
 */
function refresh() {
	$("#refresh").bind("click",function(){
		var flag = $("#flag").val();
		if ("all" == flag) {
			loadLetters(1);
		} else if ("undo" == flag) {
			loadUndoLetter(1);
		} else if ("level" == flag) {
			var level = $("#level").val();
			loadLevelLetters(1, level);
		} else if ("tag" == flag) {
			var tagName = $("#tagName").val();
			loadTagLetters(1, tagName);
		}
	});
}

/**
 * 装载未处理信件信息
 */
function loadUndoLetter(pageNum){
	var url = "/JWLetter/letter/loadUndoLetters";
	var data = {
		pageNum : pageNum
	};
	var successCallback = function(data){
		$("#flag").attr("value","undo");
		loadDatas(data);
	};
	JWUtils.ajax(url, true, data, successCallback, null);
}

/**
 * 装载等级信件信息
 */
function loadLevelLetters(pageNum,level){
	var url = "/JWLetter/letter/loadLevelLetters";
	var data = {
		pageNum : pageNum,
		level : level
	};
	var successCallback = function(data){
		$("#flag").attr("value","level");
		$("#level").attr("value",level);
		loadDatas(data);
	};
	JWUtils.ajax(url, true, data, successCallback, null);
}

/**
 * 装载标签信件信息
 */
function loadTagLetters(pageNum,tagName){
	var url = "/JWLetter/letter/loadTagLetters";
	var data = {
		pageNum : pageNum,
		tagName : tagName
	};
	var successCallback = function(data){
		$("#flag").attr("value","tag");
		$("#tag").attr("value",tagName);
		loadDatas(data);
	};
	JWUtils.ajax(url, true, data, successCallback, null);
}

/**
 * 装载返回页面的数据信息
 */
function loadDatas(data){
	$("#detail").hide();
	$("#current").attr("value",data.detail.pageResult.currentPage);
	$("#sum").attr("value",data.detail.pageResult.sumPage);
	var pageResult = data.detail.pageResult;
	if (pageResult.currentPage == pageResult.sumPage) {
		$("#recordsNums").text(((pageResult.currentPage - 1) * pageResult.perPage + 1) + "-" + pageResult.total + "/" + pageResult.total);
	} else {
		$("#recordsNums").text(((pageResult.currentPage - 1) * pageResult.perPage + 1) + "-" + pageResult.currentPage * pageResult.perPage + "/" + pageResult.total);
	}
	var letters = pageResult.records;
	var tbody = $("#letters");
	tbody.children().remove();
	if (letters.length != 0) {
		for (var i = 0; i < letters.length; i++) {
			tbody.append("<tr></tr>");
			var tr = $("#letters>tr").eq(i);
			tr.append("<td><div class='icheckbox_flat-blue' aria-checked='false' aria-disabled='false' style='position: relative;'> <input style='position: absolute; opacity: 0;' type='checkbox'> <ins class='iCheck-helper' style='position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);'></ins></div></td>");
			if (letters[i].status == 1) {
				tr.append("<td><a href='javascript:;'><i class='fa fa-star-o text-yellow'></i></a></td>");
			} else {
				tr.append("<td><a href='javascript:;'><i class='fa fa-star text-yellow'></i></a></td>");
			}
			tr.append("<td><a href='javascript:;'>"+letters[i].author+"</a></td>");
			tr.append("<td><a href='javascript:;' name='showDetail' letterId='"+letters[i].letterId+"'>"+letters[i].title+"</a></td>");
			tr.append("<td class='mailbox-attachment'></td>");
			tr.append("<td class='mailbox-date' width='10%'>"+letters[i].categoryName+"</td>");
		}
	}
	$("#message").show();
	$("a[name='showDetail']").each(function(){
		$(this).bind("click",function(){
			var letterId = $(this).attr("letterId");
			showDetail(letterId);
		});
	});
}

/**
 * 加载信件的详细信息
 */
function showDetail(letterId){
	var url = "/JWLetter/letter/loadLetterDetail";
	var data = {
		letterId : letterId
	};
	var successCallback = function(data){
		$("#message").hide();
		var letter = data.detail.letter;
		$("#title").children().remove();
		$("#title").append("<h3>"+letter.title+"</h3>");
		$("#title").append("<h5><span>"+letter.author+"</span><span class='mailbox-read-time pull-right'>"+letter.formatDate+"</span></h5>");
		$("#content").html(letter.content);
		$("#detail").show();
	};
	JWUtils.ajax(url, false, data, successCallback, null);
}